package org.itboys.core.controller.front;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.core.entity.mysql.SundayScore;
import org.itboys.core.entity.mysql.SundayScoreRecord;
import org.itboys.core.service.mysql.front.SundayFrontScoreService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.spring.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Auther: 罗尧坤
 * Date: 2018/8/21
 * Description:
 */

@Controller
@RequestMapping("/sunday/small/score")
@Api(value = "score", description = "积分")
public class SundayFrontScoreController extends BaseController {
    @Autowired
    private SundayFrontScoreService scoreService;

    /**
     * 获取积分记录
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/getRecords", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "获取积分记录")
    public void getScoreRecords(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "memberId", value = "用户id")
            @RequestParam(value = "memberId", required = true) Long memberId
            ,
            @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20")
            @RequestParam(value = "pageSize", required = true) String pageSize
            ,
            @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1")
            @RequestParam(value = "pageNumber", required = true) String pageNumber
    ) {
        try {
            List<SundayScoreRecord> records = scoreService.selectScoreRecord(request, memberId);
            ResponseMessage.success(records);
        } catch (Exception e) {

            String  msg  = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/score/selectRecords----e=" + e.getMessage());
                e.printStackTrace();
            }
            ResponseMessage.error(code, msg);
        }
    }

    /**
     * 获取个人积分总表
     *
     * @param request
     * @param response
     * @param memberId
     */
    @RequestMapping(value = "/getScore", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "获取积分主表")
    public void getScore(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "memberId", value = "用户id")
            @RequestParam(value = "memberId", required = true) Long memberId) {
        try {
            SundayScore score = scoreService.getScore(memberId);
            ResponseMessage.success(score);
        } catch (Exception e) {

            String  msg  = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/score/getScore----e=" + e.getMessage());
                e.printStackTrace();
            }
            ResponseMessage.error(code, msg);
        }
    }

}
